package tjx.Hibernate.dao;
import tjx.Hibernate.pojo.News;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import tjx.Hibernate.pojo.User;

import java.util.List;

/**
 * Author       : MadFrog
 * Date         : 2018/5/15 23:49
 * Description  :
 */
public class NewsManager {
    public static void main(String[] args) {
        // 实例化Configuration，
        Configuration conf = new Configuration()
                // 不带参数的configure()方法默认加载hibernate.cfg.xml文件，
                // 如果传入abc.xml作为参数，则不再加载hibernate.cfg.xml，改为加载abc.xml
                .configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(conf.getProperties()).build();
        // 以Configuration实例创建SessionFactory实例
        SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
        // 创建Session
        Session sess = sf.openSession();
        // 开始事务
        Transaction tx = sess.beginTransaction();
        // 创建消息对象
//        News n =(News)sess.get(News.class,1);
////        System.out.println(n.toString());
////        // 设置消息标题和消息内容
//////        n.setTitle("疯狂Java联盟成立了");
//////        n.setContent("疯狂Java联盟成立了，"
//////                + "网站地址http://www.crazyit.org");
////        n.setId(1);
////        n.setTitle("demo");
////        n.setContent("hello");
////        // 保存消息
//////        sess.save(n);
////        // 提交事务
////        sess.update(n);
//        for(int i=0;i<100000;i++)
//        {
//            User u=new User();
//            u.setUsername("root"+1);
//            sess.save(u);
//        }
        String hql="select n.user from News n where n.id=1";
        List ll=sess.createQuery(hql).list();
        Object o=ll.get(0);
        System.out.println(o.toString());
        System.out.println(ll);
        tx.commit();
        // 关闭Session
        sess.close();
        sf.close();
    }
}
